$(function(){
	var code  = getUrlParam("gridCode");
	  $.ajax({
		  url :"/autoGrid/getGrigConfig?gridCode="+code,
		  success : function(response){
			  initForm(response.data.details);
			  initGrid(response.data.details);
			 // initData(code);
		  }
	  }); 
	  $(".autoFormQuery").on("click",function(){
		  initData(code);
	  });
})

function getUrlParam(paramName) {
            var reg = new RegExp("(^|&)" + paramName + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
            var r = window.location.search.substr(1).match(reg);  //匹配目标参数
            if (r != null) return unescape(r[2]); return null; //返回参数值
}

function getDict(dictCodes){
	
}



function initForm(gridDetail){
	var dicts = [];
	var type = ["select","radio","checkbox"];
	var form = {  lableWidth:150,elementWidth:200,models:[]};
	for(var i = 0 ; i < gridDetail.length; i++){
		var detail =  gridDetail[i];
		if(!detail.isFixedQuery){
			continue;
		} 
		var model = {"field":detail.columnAlias,"lable":detail.name,"type":detail.elementType};
		//日期
		if(detail.elementType == "date"){
			var formatter = detail.formatter ? detail.formatter :'yyyy-MM-dd HH:mm:ss' 
			model.options = {css:"Wdate",events:[{event:"click",method:WdatePicker,param:{dateFmt:formatter,alwaysUseStartDate:true}}]};
		//字典
		}else if($.inArray(detail.elementType,type)){
			model.options = {data:dicts,key:"code",value:"name"};
		}
		form.models.push(model);
	} 
	 $("#autoForm").selfform(form);
	
}

function initGrid(gridDetail){
	var grid = {columns:[]};
	var dictMap = {};
	var type = ["select","radio","checkbox"];
	for(var i = 0 ; i < gridDetail.length; i++){
		var detail =  gridDetail[i];
		var column = {"field":detail.columnAlias,"title":detail.name,hidden:!detail.isDisplay};
		//日期
	    if(detail.elementType == "data"){
	    	column.formatter = function(cellValue, rowRecord, rowIndex){
	    		return cellValue ? dateformat(cellValue) : "";
	    	}
	    }else if($.inArray(detail.elementType,type)){
		/*	column.formatter = function(cellValue, rowRecord, rowIndex){
       		 var v =  dictMap[detail.dictCode][cellValue];
    		 return v || v > 0 ? v: "";
    	     } */
	     }
		grid.columns.push(column);
	} 
	$("#autoGrid").selfgrid(grid);  
}

function initData(code){ 
	var data = $("#autoForm").serialize()
	var url = "/autoGrid/query?gridCode="+code+"&"+data
	  $.ajax({
		  url : url,
		  success : function(response){
			  $("#autoGrid").selfgrid("addRows",response.data);  
		  }
	  }); 
}


